package besteAukera;

import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;

public class IrteeraDatu implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer balioa;
	private int x;
	private int y;
	private Integer lot;
public int bitKopurua;
private String datuMota;
	public IrteeraDatu(int x1, int y1, Integer bal) {
		lot = null;
		x = x1;
		y = y1;
		balioa = bal;
	}

	public void marraztu(Graphics g) {
		float[] letra = Color.RGBtoHSB(238, 238, 238, null);
		g.setColor(Color.getHSBColor(letra[0], letra[1], letra[2]));
		g.setColor(Color.red);
		
		if(balioa.toString().length()==2)
			g.drawString(balioa.toString(), x-6, y + 5);
			else if(balioa.toString().length()==3)
				g.drawString(balioa.toString(), x-12, y + 5);
			else 	g.drawString(balioa.toString(), x, y + 5);
	}
	public void setBitKopurua(int kopurua){
		bitKopurua=kopurua;
	}
	public int getBitKopurua(){
		return bitKopurua;
	}
	public void setDatuMota(String datMot){
		datuMota=datMot;
	}
	public void ezabatu(Graphics g) {
		g.clearRect(x, y + 10, 20, 20);
	}

	public Integer getBalioa() {
		return balioa;
	}

	public void setBalioa(Integer bal) {
		if(datuMota.compareTo("Positiboa")==0){
		if ((bal>Math.pow(2, bitKopurua)-1)||(bal<0))
			bal=bal-(int)Math.pow(2, bitKopurua);
		}
		
		else if(datuMota.compareTo("BirakoOs")==0){
			
			
			if(bal>Math.pow(2, bitKopurua-1)-1)
			{if(bitKopurua>1)
				bal=bal-(int)Math.pow(2, bitKopurua);
			}
			if (bal<-Math.pow(2, bitKopurua-1))
				bal=bal+(int)Math.pow(2, bitKopurua);
		}
		balioa = bal;
	}

	public void setLotura(Integer l) {
		this.lot = l;
	}

	public boolean loturaDu() {
		return (lot == null);
	}

	public void setPosizioa(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public boolean estaDentro(int x, int y) {
		if ((x > this.x) && (x < (this.x + 20)) && (y > this.y)
				&& (y + 10 < (this.y + 20))) {
			return true;
		}
		return false;
	}

	public void loturaEzabatu() {
		lot = null;
	}
}
